package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.ModelRule;
import com.scau.model.business.OrderList;
import com.scau.model.business.OrderShoe;
import com.scau.model.business.Shoe;
import com.scau.model.view.OrderShoeView;
import com.scau.service.impl.business.ModelRuleService;
import com.scau.service.impl.business.OrderListService;
import com.scau.service.impl.business.OrderShoeService;
import com.scau.service.impl.business.ShoeService;
import com.scau.service.impl.view.OrderShoeViewService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class OrderShoeAction extends BaseAction implements ModelDriven<OrderShoe>,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private OrderShoeService orderShoeService = null;
	private OrderShoe orderShoe = null;
	private OrderShoeViewService orderShoeViewService = null;
	private OrderShoeView orderShoeView = null;
	private OrderListService orderListService;
	private ShoeService shoeService;
	private ModelRuleService modelRuleService;
	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		String order_id = request.getParameter("order_id");
		if (null != order_id && !"".equals(order_id)) {
			request.setAttribute("order_id", order_id);
		}
		
		
		OrderShoeView orderShoeView = new OrderShoeView();
		orderShoeView.setOrder_id(Long.parseLong(order_id));
		int totalRows = orderShoeViewService
				.getRecordCount(orderShoeView);
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<OrderShoeView> resourceList = orderShoeViewService.list(
				new OrderShoeView(), this.pager.getPageStartRow(),
				pager.getPageSize(),new String[]{"order_id"},new Object[]{order_id});
		this.pager.setData(resourceList);
		OrderList orderList = new OrderList();
		orderList.setId(Long.parseLong(order_id));
		orderList = orderListService.get(orderList);
		request.setAttribute("orderList", orderList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		orderShoe = orderShoeService.get(orderShoe);
		List<Shoe> shoeList = shoeService.listAll(new Shoe());
		request.setAttribute("shoeList", shoeList);
		List<ModelRule> modelRuleList = modelRuleService
				.listAll(new ModelRule());
		request.setAttribute("modelRuleList", modelRuleList);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			if (null != request.getParameter("status")) {
				orderShoe = orderShoeService.get(orderShoe);
				orderShoe.setTransact_status(1L);
			}
			orderShoeService.save(orderShoe);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("orderShoe", orderShoe);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			OrderShoe orderShoe = new OrderShoe();// CommUser user =
			// new CommUser();
			if (null != id && !("".equals(id))) {
				orderShoe.setId(Long.parseLong(id));
				orderShoeService.delete(orderShoe);
			}
		}
		return list();
	}

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public OrderShoe getOrderShoe() {
		return this.orderShoe;
	}

	public void setOrderShoe(OrderShoe orderShoe) {
		this.orderShoe = orderShoe;
	}

	@Resource
	public void setOrderShoeService(OrderShoeService orderShoeService) {
		this.orderShoeService = orderShoeService;
	}

	public OrderShoeViewService getOrderShoeViewService() {
		return orderShoeViewService;
	}

	@Resource
	public void setOrderShoeViewService(
			OrderShoeViewService orderShoeViewService) {
		this.orderShoeViewService = orderShoeViewService;
	}

	public OrderShoeService getOrderShoeService() {
		return orderShoeService;
	}

	public OrderListService getOrderListService() {
		return orderListService;
	}

	@Resource
	public void setOrderListService(OrderListService orderListService) {
		this.orderListService = orderListService;
	}

	public ShoeService getShoeService() {
		return shoeService;
	}

	@Resource
	public void setShoeService(ShoeService shoeService) {
		this.shoeService = shoeService;
	}

	public ModelRuleService getModelRuleService() {
		return modelRuleService;
	}

	@Resource
	public void setModelRuleService(ModelRuleService modelRuleService) {
		this.modelRuleService = modelRuleService;
	}

	public OrderShoeView getOrderShoeView() {
		return orderShoeView;
	}

	public void setOrderShoeView(OrderShoeView orderShoeView) {
		this.orderShoeView = orderShoeView;
	}

	@Override
	public OrderShoe getModel() {
		// TODO Auto-generated method stub
		return orderShoe;
	}

}
